/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
// linuxpeg.hpp - Macro definitions, control types and prototypes for
//                the multithreaded Linux integration.
//
// Author: Jim DeLisle
//
// Copyright (c) 1997-2007 Swell Software, Inc.
//              All Rights Reserved.
//
// Unauthorized redistribution of this source code, in whole or part,
// without the express written permission of Swell Software, Inc.
// is strictly prohibited.
//
/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/

#ifndef LINUXPEG_HPP
#define LINUXPEG_HPP

/*---------------------------------------------------------------------------*/
// There are 2 ways to run the timer under Linux: POSIX, which uses
// timers, semaphores, and signals; and, sleeps, which use some sort
// of delay mechanism. Both styles run in a thread which is separate
// from the main PEG thread.
//
// The default implementation is POSIX; but, under some circumstances (such
// as the Linux BSP for your target not supported all of the POSIX spec)
// it is preferable to use the sleep method.
//
// Turning on the define below will implement the POSIX method.
/*---------------------------------------------------------------------------*/
//#define PEG_POSIX_TIMER

/*---------------------------------------------------------------------------*/
// Defines for PEG task types
/*---------------------------------------------------------------------------*/
#define PEG_TASK_TYPE       pthread_t
#define PEG_MUTEX_TYPE      pthread_mutex_t

/*---------------------------------------------------------------------------*/
// The message pool and queue types
/*---------------------------------------------------------------------------*/
struct PegMessage;  // forward reference

struct PegLinuxPool
{
    PegMessage *first;
    PegMessage *last;
};

struct PegLinuxQueue
{
    PegMessage *first;
    PegMessage *last;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
    int count;
};

#define PEG_QUEUE_TYPE        PegLinuxQueue*
#define PEG_QUEUE_NULL      ((PEG_QUEUE_TYPE) NULL)

/*---------------------------------------------------------------------------*/
// Externs
/*---------------------------------------------------------------------------*/
extern pthread_t          gPegMainThread;

extern unsigned int           gCustomPScreenBufferAddr;
extern unsigned int           gCustomPScreenBufferWidth;
extern unsigned int           gCustomPScreenBufferHeight;
extern unsigned int           gCustomPScreenBufferBPP;

extern unsigned int           gDisableTouchUsage;
extern unsigned int           gDisableMiscUsage;
extern unsigned int           gDisableMouseUsage;

int SetMinMax(int devNum, int XMin, int XMax, int YMin,int YMax, int screenWidth, int screenHeight);
void UnCalibrate(int devNum);
/*---------------------------------------------------------------------------*/
// Prototypes
/*---------------------------------------------------------------------------*/
void EnterCriticalSection();
void ExitCriticalSection();
void LockMessageQueue();
void UnlockMessageQueue();
void LockTimerList();
void UnlockTimerList();

PEG_QUEUE_TYPE CreateMsgQueue();
void DeleteMsgQueue(PEG_QUEUE_TYPE tQueue);
void EnqueueTaskMessage(void* pMesg, PEG_QUEUE_TYPE tQueue);

/*---------------------------------------------------------------------------*/
// Multithread defines
/*---------------------------------------------------------------------------*/
#define LOCK_PEG                    EnterCriticalSection();
#define UNLOCK_PEG                  ExitCriticalSection();
#define LOCK_MESSAGE_QUEUE          LockMessageQueue();
#define UNLOCK_MESSAGE_QUEUE        UnlockMessageQueue();
#define LOCK_TIMER_LIST             LockTimerList();
#define UNLOCK_TIMER_LIST           UnlockTimerList();
#define CURRENT_TASK                pthread_self()
#define PEG_TASK_PTR                gPegMainThread
#define CREATE_MESG_QUEUE           CreateMsgQueue()
#define DELETE_MESSAGE_QUEUE(a)     DeleteMsgQueue(a)
#define ENQUEUE_TASK_MESSAGE(a, b)  EnqueueTaskMessage(a, b)
#define PEG_DELAY(msecs)            usleep(1000 * msecs)

#define LINUXPEG_PRINTF_INFO    0
#define LINUXPEG_PRINTF_WARNING 1
#define LINUXPEG_PRINTF_ERROR   2
#define LINUXPEG_PRINTF_DEBUG   3

//set this to the level of message you want to see printed.
#define LINUXPEG_PRINTF_LEVEL LINUXPEG_PRINTF_ERROR

#endif // LINUXPEG_HPP_

